//加载类
const Loading = {
	/**
	 * @param {string} title 标题
	 **/
	show: function(title) {
		uni.showLoading({
			title
		})
	},
	/**
	 * @description 关闭加载
	 **/
	hidden: function() {
		uni.hideLoading()
	}
}
//提示类
const Toast = {
	success: function(title) {
		this.toast(title, 'success')
	},
	error: function(title) {
		this.toast(title, 'error')
	},
	none: function(title) {
		this.toast(title, 'none')
	},
	/**
	 * @param {string} title 标题
	 * @param {string} icon 图标 success/error/none
	 **/
	toast: function(title, icon) {
		uni.showToast({
			title,
			icon
		})
	}
}
//路由跳转
const Routes = {
	/**
	 * @param {string} url 页面路径
	 **/
	push: function(url, options = {}) {
		uni.navigateTo({
			url,
			events: options.events || {},
			success(res) {
				if (options.callback) options.callback(res)
			}
		})
	},
	tab: function(url) {
		uni.switchTab({
			url
		})

	},
	lunch: function(url) {
		uni.reLaunch({
			url
		})

	},
	rect: function(url) {
		uni.redirectTo({
			url
		})
	},
	back: function(delta = 1) {
		uni.navigateBack({
			delta
		})
	}
}
//微信登录相关
const WxBaseApi = {
	getCode: function() {
		return new Promise((r, j) => {
			uni.login({
				provider: 'weixin',
				success(e) {
					r(e.code)
				},
				fail(error) {
					j(error)
				}
			})
		})
	}
}
//Uni相关
const UniBaseApi = {
	copy: function(data) {
		uni.setClipboardData({
			data
		})
	},
	/** 图片处理-选择图片
	 * @param {Number} count - 选择数量
	 * @param {String} sourceType - 选择方式，album 相册,camera 相机
	 * @param {String} sizeType - 图片质量，original 原图,compressed 压缩
	 **/
	chooseImage: (count = 1, sourceType = ['album', 'camera'], sizeType = ['original', 'compressed']) => {
		return new Promise((resolve, reject) => {
			sourceType = Object.prototype.toString.call(sourceType) === '[object String]' ? [
				sourceType
			] : sourceType
			sizeType = Object.prototype.toString.call(sizeType) === '[object String]' ? [
				sizeType
			] : sizeType
			uni.chooseImage({
				count,
				sourceType,
				sizeType,
				success: res => {
					resolve(res.tempFilePaths);
				}
			})
		})
	},
	/** 图片处理-上传图片，暂支支持单张上传
	 * @param {String} url - 上传服务器接口
	 * @param {String,Array} tempFilePaths - 图片资源
	 * @param {String} name - 服务器接收资源对应字段
	 **/
	uploadImage: (url, tempFilePaths = [], name = 'file') => {
		return new Promise((r, j) => {
			uni.showLoading({
				title: '上传中...'
			})
			uni.uploadFile({
				url,
				filePath: tempFilePaths[0],
				name,
				header: {
					token: uni.getStorageSync('token')
				},
				success: uploadFileRes => {
					r(JSON.parse(uploadFileRes.data).data)
				},
				complete() {
					uni.hideLoading()
				}
			});
		})
	},
}
export {
	Loading,
	Toast,
	Routes,
	WxBaseApi,
	UniBaseApi
}